package com.souku.finance.application.ui.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.souku.finance.application.R;
import com.souku.finance.application.adapter.BalanceDetailedAdapter;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.bean.BalanceDetailedBean;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.SuccessError;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Android on 2016/12/25.
 */

public class BalanceDetailedActivity extends BaseActivity implements View.OnClickListener {

    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected PullToRefreshListView pullList;
    private BalanceDetailedAdapter adapter;
    private int page = 1;
    private List<BalanceDetailedBean.ListBean> beans = new ArrayList<>();

    @Override
    public void internet() {
        pullList.setRefreshing(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        pullList.setRefreshing(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.act_balancedetailed);
        initView();
        topTitle.setText("收支明细");
        adapter = new BalanceDetailedAdapter(beans);
        pullList.setAdapter(adapter);
        pullList.setMode(PullToRefreshBase.Mode.BOTH);
        pullList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                beans.clear();
                getUserAccTansLog();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                getUserAccTansLog();
            }
        });

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.top_back) {
            finish();
        }
    }

    private void initView() {
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(BalanceDetailedActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        pullList = (PullToRefreshListView) findViewById(R.id.pull_list);
    }

    private void getUserAccTansLog() {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("pg_no", page + "");
        RequestManager.getInstance().post(UrlConstact.USER_ACC_TANS_LOG, ParamUtil.getSing(map), new ReqListener(this, new SuccessError() {
            @Override
            public void onSuccess(String response) {
                pullList.onRefreshComplete();
                if (JsonUtil.getCode(response) == 1) {
                    BalanceDetailedBean bean = JsonUtil.fastBean(response, BalanceDetailedBean.class);
//                    beans = new JsonUtil().fastArrayBean(response, BalanceDetailedBean.ListBean.class);
                    if(bean.getList().size()==0){
//                    if (beans.size() == 0) {
                        ToastUtil.show("没有更多内容");
                    } else {
                        beans.addAll(bean.getList());
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }

            @Override
            public void onError(String errorMsg) {
                ToastUtil.show("请求失败");
                pullList.onRefreshComplete();
            }
        }), ParamUtil.getHeaders());
    }
}
